package org.jeecg.modules.base.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.base.entity.BaseBook;
import org.jeecg.modules.base.entity.BaseScore;
import org.jeecg.modules.base.mapper.BaseBookMapper;
import org.jeecg.modules.base.service.IBaseBookService;
import org.jeecg.modules.base.service.IBaseScoreService;
import org.jeecg.modules.online.config.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;

/**
 * @Description: base_book
 * @Author: jeecg-boot
 * @Date:   2021-04-14
 * @Version: V1.0
 */
@Service
public class BaseBookServiceImpl extends ServiceImpl<BaseBookMapper, BaseBook> implements IBaseBookService {

    @Autowired
    private IBaseScoreService scoreService;
    @Override
    public Result borrow(String id) {
        BaseBook book = baseMapper.selectById(id);
        if("否".equals(book.getIsBorrow())){
            return Result.error("该图书已经被借阅!");
        }else{
            book.setIsBorrow("否");
            baseMapper.updateById(book);
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            BaseScore score = new BaseScore();
            score.setBookId(book.getId());
            score.setBorrowTime(new Date());
            score.setUserId(sysUser.getId());
            scoreService.save(score);
            return Result.OK("借阅成功！");
        }
    }
}
